package com.xlf.service.base.service.impl.validate;

import com.xlf.common.core.exception.BizException;
import com.xlf.common.core.util.StringUtils;
import jodd.util.StringUtil;

public class AccountValidator {

    private static final int MIN_USERNAME_LENGTH = 5;
    private static final int MAX_USERNAME_LENGTH = 10;

    private static final int MIN_PASSWORD_LENGTH = 5;
    private static final int MAX_PASSWORD_LENGTH = 15;

    public static final String registerLock = "registerLock:";


//    private static final String SPECIAL_CHARACTERS_REGEX = "[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]";
//    private static final String UPPER_CASE_REGEX = "[A-Z]+";
//    private static final String LOWER_CASE_REGEX = "[a-z]+";
//    private static final String NUMBER_REGEX = "\\d+";

    /**
     * 只能包括Ascii
     * @param str
     * @return
     */
    public static boolean isValidAscii(String str) {
        if (str == null) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) > 127) {
                return false;
            }
        }
        return true;
    }

    /**
     * 账号校验
     * @param username
     * @return
     */
    public static boolean isValidUsername(String username) {

        if(StringUtils.isEmpty(username))
            throw new BizException("账号不能为空");

        if(!isValidAscii(username))
            throw new BizException("账号只能包括Ascii字符");

        if(username.length() < MIN_USERNAME_LENGTH)
            throw new BizException("账号长度应大于等于" + MAX_USERNAME_LENGTH +"个合法字符");

        if(username.length() > MAX_USERNAME_LENGTH)
            throw new BizException("账号长度应小于等于" + MAX_USERNAME_LENGTH +"个合法字符");

        return true;
    }

    /**
     * 密码校验
     * @param password
     * @return
     */
    public static boolean isValidPassword(String password) {

        if(StringUtils.isEmpty(password))
            throw new BizException("密码不能为空");

        if(!isValidAscii(password))
            throw new BizException("密码只能包括Ascii字符");

        if(password.length() < MIN_PASSWORD_LENGTH)
            throw new BizException("密码长度应大于等于" + MAX_USERNAME_LENGTH  +"个合法字符");

        if(password.length() > MAX_PASSWORD_LENGTH)
            throw new BizException("密码长度应小于等于" + MAX_USERNAME_LENGTH +"个合法字符");

        return true;
    }
}
